/**
 * 
 */
package com.gamiao.gwt.demo.remoteServiceDemo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Administrator
 * 
 */
public class RemoteServiceDemo implements EntryPoint {
	private TextBox commentTextBox = new TextBox();
	private Button sendButton = new Button("Commit comment & Get server time");

	private Label text_clickTimesIs = new Label("Click times is:");
	private Label text_serverTimeIs = new Label(
			"The time of Gamiao's server is:");
	private Label text_commentIs = new Label("Your comment is:");
	private Label text_systemRemark = new Label("System remark:");
	private HTML html = new HTML("<br/>");
	private Label clickTimes = new Label("0");
	private Label serverTime = new Label(
			"Click button to get the first server time.");
	private Label commentIs = new Label("");
	private Label sysRemark = new Label("Nothing Yet.");

	private FlexTable flexTable = new FlexTable();

	public void onModuleLoad() {
		commentTextBox.addFocusListener(new FocusListener() {
			public void onFocus(Widget sender) {
				if (commentTextBox.getText().equals("Please enter some word.")) {
					commentTextBox.setText("");
				}
			};

			public void onLostFocus(Widget sender) {
				if (commentTextBox.getText().equals("")) {
					commentTextBox.setText("Please enter some word.");
				}
			};
		});

		flexTable.setTitle("TestTable");
		flexTable.setStyleName("crystal");
		flexTable.setCellPadding(2);
		flexTable.setCellSpacing(2);
		flexTable.setHTML(0, 0, "Times of click");
		flexTable.setHTML(0, 1, "Return server time");
		flexTable.setHTML(0, 2, "Comment");
		flexTable.getRowFormatter().setStyleName(0, "head");

		sendButton.addClickListener(new SenderButtonClickListener());

		RootPanel.get().add(commentTextBox);
		RootPanel.get().add(sendButton);
		RootPanel.get().add(html);

		RootPanel.get().add(text_clickTimesIs);
		RootPanel.get().add(clickTimes);
		RootPanel.get().add(html);

		RootPanel.get().add(text_serverTimeIs);
		RootPanel.get().add(serverTime);
		RootPanel.get().add(html);

		RootPanel.get().add(text_commentIs);
		RootPanel.get().add(commentIs);
		RootPanel.get().add(html);

		RootPanel.get().add(text_systemRemark);
		RootPanel.get().add(sysRemark);
		RootPanel.get().add(html);

		RootPanel.get().add(flexTable);
	}

	public class SenderButtonClickListener implements ClickListener {
		public void onClick(Widget sender) {
			RemoteServiceDemoServletAsync async = RemoteServiceDemoServlet.Util
					.getInstance();
			int timesOfClick = Integer.parseInt(clickTimes.getText()) + 1;
			async.getComment(timesOfClick, commentTextBox.getText(),
					new SyncServletCallback());
		}
	}

	public class SyncServletCallback implements AsyncCallback {

		public void onFailure(Throwable error) {
			sysRemark.setText("Operation Failed!");
		}

		public void onSuccess(Object resp) {
			String[] strs = (String[]) resp;
			String clickTimesReturn = (String) strs[0];
			String timesReturn = (String) strs[1];
			String commentReturn = (String) strs[2];

			clickTimes.setText(clickTimesReturn);
			serverTime.setText(commentReturn);
			commentIs.setText(timesReturn);
			sysRemark.setText("Operation Success!");

			int rowNumber = flexTable.getRowCount();
			flexTable.setText(rowNumber, 0, clickTimesReturn);
			flexTable.setText(rowNumber, 1, timesReturn);
			flexTable.setText(rowNumber, 2, commentReturn);
			flexTable.getRowFormatter().setStyleName(rowNumber,
					rowNumber % 2 == 0 ? "even" : "odd");
		}
	}
}